<?php

namespace App\Http\Controllers;

use App\Http\Biz\SuppOrderBiz;
use App\Http\Model\ConsoleAccount;
use App\Http\Model\ConsoleAccountRole;
use App\Http\Model\ShopTorageInOut;
use App\Http\Model\Supp;
use App\Http\Model\SuppPurchase;
use App\Http\Utils\Common\Consts;
use App\Http\Utils\Common\DictUtils;
use App\Http\Utils\Common\OperResult;
use App\Http\Utils\Common\RoleUtils;
use App\Http\Utils\Common\WechatUtils;
use App\Http\Utils\ControllerTrait\WebUploadTrait;
use App\Http\Utils\ControllerTrait\WechatQrCodeTrait;
use Carbon\Carbon;
use GuzzleHttp\Client;
use Illuminate\Http\Request;
use Log;
use ErrorException;

class SuppOrderController extends Controller
{
    use WebUploadTrait, WechatQrCodeTrait;

    public function mainData(Request $request)
    {
        try {
            Log::debug(get_class($this) . ' ' . __FUNCTION__ . ' ' . 'start');
            if (!$request->has('supp_id') || !$request->has('account_id'))
                return response()->json(['code' => OperResult::CODE_CREATE_FAIL, 'msg' => OperResult::CODE_CREATE_FAIL_DESC, 'data' => array()]);
            $tmpBiz = new SuppOrderBiz();
            $data = $tmpBiz->mainDataBiz($request->supp_id, $request->account_id);
            Log::debug(get_class($this) . ' ' . __FUNCTION__ . ' ' . 'end');
            return response()->json(['code' => OperResult::CODE_READ_OK, 'msg' => OperResult::CODE_READ_OK_DESC, 'data' => $data]);
        } catch (ErrorException $ex) {
            Log::error(get_class($this) . ' exception ' . print_r($ex->getMessage(), true));
            return response()->json(['code' => OperResult::CODE_READ_FAIL, 'msg' => OperResult::CODE_READ_FAIL_DESC, 'data' => array()]);
        }
    }

    //销售发货的提货点列表
    public function readPickupByDay(Request $request)
    {
        try {
            Log::debug(get_class($this) . ' ' . __FUNCTION__ . ' ' . 'start');
            if (!$request->has('supp_id') || !$request->has('year') || !$request->has('month') || !$request->has('day'))
                return response()->json(['code' => OperResult::CODE_CREATE_FAIL, 'msg' => OperResult::CODE_CREATE_FAIL_DESC, 'data' => array()]);
            $tmpBiz = new SuppOrderBiz();
            $data = $tmpBiz->readPickupByDayBiz($request->supp_id, $request->year, $request->month, $request->day, $request->serial_num,$request->account_id);
            Log::debug(get_class($this) . ' ' . __FUNCTION__ . ' ' . 'end');
            return response()->json(['code' => OperResult::CODE_READ_OK, 'msg' => OperResult::CODE_READ_OK_DESC, 'data' => $data]);
        } catch (ErrorException $ex) {
            Log::error(get_class($this) . ' exception ' . print_r($ex->getMessage(), true));
            return response()->json(['code' => OperResult::CODE_READ_FAIL, 'msg' => OperResult::CODE_READ_FAIL_DESC, 'data' => array()]);
        }
    }


    //根据日期查询详细的采购列表数据
    public function readSaleListByDay(Request $request)
    {
        try {
            Log::debug(get_class($this) . ' ' . __FUNCTION__ . ' ' . 'start');
            if (!$request->has('supp_id') || !$request->has('purchase_id'))
                return response()->json(['code' => OperResult::CODE_CREATE_FAIL, 'msg' => OperResult::CODE_CREATE_FAIL_DESC, 'data' => array()]);
            $tmpBiz = new SuppOrderBiz();
            $data = $tmpBiz->readSaleListByDayBiz($request->supp_id, $request->purchase_id, $request->account_id);
            Log::debug(get_class($this) . ' ' . __FUNCTION__ . ' ' . 'end');
            return response()->json(['code' => OperResult::CODE_READ_OK, 'msg' => OperResult::CODE_READ_OK_DESC, 'data' => $data]);
        } catch (ErrorException $ex) {
            Log::error(get_class($this) . ' exception ' . print_r($ex->getMessage(), true));
            return response()->json(['code' => OperResult::CODE_READ_FAIL, 'msg' => OperResult::CODE_READ_FAIL_DESC, 'data' => array()]);
        }
    }

    //按采购表查询所有日期列表接口（分页）
    public function readSaleDataByPage(Request $request)
    {
        try {
            Log::debug(get_class($this) . ' ' . __FUNCTION__ . ' ' . 'start');
            if (!$request->has('supp_id'))
                return response()->json(['code' => OperResult::CODE_CREATE_FAIL, 'msg' => OperResult::CODE_CREATE_FAIL_DESC, 'data' => null]);
            $tmpBiz = new SuppOrderBiz();
            $data = $tmpBiz->readSaleDataByPageBiz($request->supp_id,$request->account_id);
            Log::debug(get_class($this) . ' ' . __FUNCTION__ . ' ' . 'end');
            return response()->json(['code' => OperResult::CODE_READ_OK, 'msg' => OperResult::CODE_READ_OK_DESC, 'data' => $data]);
        } catch (ErrorException $ex) {
            Log::error(get_class($this) . ' exception ' . print_r($ex->getMessage(), true));
            return response()->json(['code' => OperResult::CODE_READ_FAIL, 'msg' => OperResult::CODE_READ_FAIL_DESC, 'data' => null]);
        }
    }

    //按天查询商品总订单数和总金额列表接口
    public function readOrderByDate(Request $request)
    {
        try {
            Log::debug(get_class($this) . ' ' . __FUNCTION__ . ' ' . 'start');
            if (!$request->has('supp_id') || !$request->has('year') || !$request->has('month') || !$request->has('day'))
                return response()->json(['code' => OperResult::CODE_CREATE_FAIL, 'msg' => OperResult::CODE_CREATE_FAIL_DESC, 'data' => array()]);
            $tmpBiz = new SuppOrderBiz();
            $data = $tmpBiz->readOrderByDateBiz($request->supp_id, $request->year, $request->month, $request->day,$request->account_id);
            Log::debug(get_class($this) . ' ' . __FUNCTION__ . ' ' . 'end');
            return response()->json(['code' => OperResult::CODE_READ_OK, 'msg' => OperResult::CODE_READ_OK_DESC, 'data' => $data]);
        } catch (ErrorException $ex) {
            Log::error(get_class($this) . ' exception ' . print_r($ex->getMessage(), true));
            return response()->json(['code' => OperResult::CODE_READ_FAIL, 'msg' => OperResult::CODE_READ_FAIL_DESC, 'data' => array()]);
        }
    }

    //按天查询统计列表（分页）接口，展示形式：日期 订单总数 订单总额
    public function readOrderDateByPage(Request $request)
    {
        try {
            Log::debug(get_class($this) . ' ' . __FUNCTION__ . ' ' . 'start');
            if (!$request->has('supp_id'))
                return response()->json(['code' => OperResult::CODE_CREATE_FAIL, 'msg' => OperResult::CODE_CREATE_FAIL_DESC, 'data' => null]);
            $tmpBiz = new SuppOrderBiz();
            $data = $tmpBiz->readOrderDateByPageBiz($request->supp_id,$request->account_id);
            Log::debug(get_class($this) . ' ' . __FUNCTION__ . ' ' . 'end');
            return response()->json(['code' => OperResult::CODE_READ_OK, 'msg' => OperResult::CODE_READ_OK_DESC, 'data' => $data]);
        } catch (ErrorException $ex) {
            Log::error(get_class($this) . ' exception ' . print_r($ex->getMessage(), true));
            return response()->json(['code' => OperResult::CODE_READ_FAIL, 'msg' => OperResult::CODE_READ_FAIL_DESC, 'data' => null]);
        }
    }


    public function getOpenid(Request $request)
    {
        try {
            $code = $request->code;
            $client = new Client();
            $res = $client->request('GET', 'https://api.weixin.qq.com/sns/jscode2session?appid=wxedf294ed867d3f2f&secret=29725906c00b5e3054d0c9cb89cf1599&js_code=' . $code . '&grant_type=authorization_code');
            $json = $res->getBody();
            return $json;
        } catch (ErrorException $ex) {
            Log::error(get_class($this) . ' exception ' . print_r($ex->getMessage(), true));
            return response()->json(['code' => OperResult::CODE_READ_FAIL, 'msg' => OperResult::CODE_READ_FAIL_DESC, 'data' => array()]);
        }

    }

    //修改素材信息
    public function updatEmaterial(Request $request)
    {
        try {
            Log::debug(get_class($this) . ' ' . __FUNCTION__ . ' ' . 'start');
            if (!$request->has('supp'))
                return response()->json(['code' => OperResult::CODE_CREATE_FAIL, 'msg' => OperResult::CODE_CREATE_FAIL_DESC, 'data' => array()]);
            $params = $request->all()['supp'];
            $params = collect($params)->filter(function ($item) {
                return $item != '';
            })->all();
            $tmpBiz = new SuppOrderBiz();
            $tmpBiz->updatEmaterial($params);
            Log::debug(get_class($this) . ' ' . __FUNCTION__ . ' ' . 'end');
            return response()->json(['code' => OperResult::CODE_READ_OK, 'msg' => OperResult::CODE_READ_OK_DESC, 'data' => []]);
        } catch (ErrorException $ex) {
            Log::error(get_class($this) . ' exception ' . print_r($ex->getMessage(), true));
            return response()->json(['code' => OperResult::CODE_READ_FAIL, 'msg' => OperResult::CODE_READ_FAIL_DESC, 'data' => array()]);
        }
    }

    //查询所有期数的信息列表
    public function readAllNperData(Request $request)
    {
        try {
            Log::debug(get_class($this) . ' ' . __FUNCTION__ . ' ' . 'start');
            if (!$request->has('supp_id'))
                return response()->json(['code' => OperResult::CODE_CREATE_FAIL, 'msg' => OperResult::CODE_CREATE_FAIL_DESC, 'data' => null, 'dict' => []]);
            $nper_id = $request->input('nper_id', null);
            $tmpBiz = new SuppOrderBiz();
            $data = $tmpBiz->readAllNperDataBiz($request->supp_id, $nper_id);
            $nperList = $tmpBiz->readAllNperBiz($request->supp_id);
            $dict = DictUtils::getDict('status', '供应商期数发货状态');
            Log::debug(get_class($this) . ' ' . __FUNCTION__ . ' ' . 'end');
            return response()->json(['code' => OperResult::CODE_READ_OK, 'msg' => OperResult::CODE_READ_OK_DESC, 'data' => $data, 'dict' => $dict, 'nperList' => $nperList]);
        } catch (ErrorException $ex) {
            Log::error(get_class($this) . ' exception ' . print_r($ex->getMessage(), true));
            return response()->json(['code' => OperResult::CODE_READ_FAIL, 'msg' => OperResult::CODE_READ_FAIL_DESC, 'data' => null, 'dict' => []]);
        }
    }

    //按期数查询所有订单列表
    public function readOrderMsgByNperId(Request $request)
    {
        try {
            Log::debug(get_class($this) . ' ' . __FUNCTION__ . ' ' . 'start');
            if (!$request->has('supp_id') || !$request->has('nper_id'))
                return response()->json(['code' => OperResult::CODE_CREATE_FAIL, 'msg' => OperResult::CODE_CREATE_FAIL_DESC, 'data' => []]);

            $tmpBiz = new SuppOrderBiz();
            $data = $tmpBiz->readOrderMsgByNperIdBiz($request->supp_id, $request->nper_id);
            Log::debug(get_class($this) . ' ' . __FUNCTION__ . ' ' . 'end');
            return response()->json(['code' => OperResult::CODE_READ_OK, 'msg' => OperResult::CODE_READ_OK_DESC, 'data' => $data]);
        } catch (ErrorException $ex) {
            Log::error(get_class($this) . ' exception ' . print_r($ex->getMessage(), true));
            return response()->json(['code' => OperResult::CODE_READ_FAIL, 'msg' => OperResult::CODE_READ_FAIL_DESC, 'data' => []]);
        }
    }

    //发货接口
    public function sendOrderGoods(Request $request)
    {
        try {
            Log::debug(get_class($this) . ' ' . __FUNCTION__ . ' ' . 'start');
            if (!$request->has('supp_id') || !$request->has('pickup_ids') || !$request->has('purchase_id'))
                return response()->json(['code' => OperResult::CODE_CREATE_FAIL, 'msg' => OperResult::CODE_CREATE_FAIL_DESC, 'data' => null]);

            $tmpBiz = new SuppOrderBiz();
            $data = $tmpBiz->sendOrderGoodsBiz($request->supp_id, $request->purchase_id, $request->pickup_ids);
//            if ($data == 2) {
//                if(count($request->pickup_ids) > 0){
//                    try {
//                        $client = new Client();
//                        $response = $client->request('POST', Consts::SYS_SHOP_URL . '/shop/push_template_msg', [
//                            'form_params' => [
//                                'supp_id' => $request->supp_id,
//                                'purchase_id' => $request->purchase_id,
//                                'pickup_ids' => $request->pickup_ids
//                            ]
//                        ]);
//                    } catch (ErrorException $err_ex) {
//                        Log::info('推送失败');
//                        Log::info($err_ex);
//                    } catch (Exception $ex) {
//                        Log::info('推送失败');
//                    }
//                }
//            }
            Log::debug(get_class($this) . ' ' . __FUNCTION__ . ' ' . 'end');
            return response()->json(['code' => $data, 'msg' => OperResult::CODE_READ_OK_DESC]);
        } catch (ErrorException $ex) {
            Log::error(get_class($this) . ' exception ' . print_r($ex->getMessage(), true));
            return response()->json(['code' => OperResult::CODE_READ_FAIL, 'msg' => OperResult::CODE_READ_FAIL_DESC, 'data' => null]);
        }
    }


    public function send($supp_id, $purchase_id, array $pickup_ids)
    {
        try {

            $client = new Client();
            $response = $client->request('POST', Consts::SYS_SHOP_URL . '/shop/push_template_msg', [
                'form_params' => [
                    'supp_id' => $supp_id,
                    'purchase_id' => $purchase_id,
                    'pickup_ids' => $pickup_ids
                ]
            ]);
//            Log::info($response);
//                $util = new WechatUtils();
//                $util->sendTemplate($complate_id, $own_id, $account_type, $openids,$params,$page);
        } catch (ErrorException $err_ex) {
//                $err_push_touser[] = ['err'=>$err_ex->getMessage(), 'touser'=>$p['touser']];
            Log::info('推送失败');
        } catch (Exception $ex) {
            Log::info('推送失败');
//                $err_push_touser[] = ['err'=>$ex->getMessage(), 'touser'=>$p['touser']];
        }

    }

    //读取所有的期数
    public function readAllNper(Request $request)
    {
        try {
            Log::debug(get_class($this) . ' ' . __FUNCTION__ . ' ' . 'start');
            if (!$request->has('supp_id'))
                return response()->json(['code' => OperResult::CODE_CREATE_FAIL, 'msg' => OperResult::CODE_CREATE_FAIL_DESC, 'data' => []]);
            $tmpBiz = new SuppOrderBiz();
            $data = $tmpBiz->readAllNperBiz($request->supp_id);
            Log::debug(get_class($this) . ' ' . __FUNCTION__ . ' ' . 'end');
            return response()->json(['code' => OperResult::CODE_READ_OK, 'msg' => OperResult::CODE_READ_OK_DESC, 'data' => $data]);
        } catch (ErrorException $ex) {
            Log::error(get_class($this) . ' exception ' . print_r($ex->getMessage(), true));
            return response()->json(['code' => OperResult::CODE_READ_FAIL, 'msg' => OperResult::CODE_READ_FAIL_DESC, 'data' => []]);
        }
    }

}
